<?php

/**
 * @author gbfun <gbfun@163.com>  2018-05-25 16:21
 * http://www.wuweiyou.com
 */

class wwy_page_handler
{ 
    protected static $_instance = null;
    
    private function __clone()
    {
    }
    
    /**
     * @return wwy_page_handler
     */
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    protected function __construct()
    {
        
    }
    
    public function get_page_data($total_item_count, $page_item_count, $current_page_num, $page_range_count)
    {
        $total_item_count = intval($total_item_count);
        $page_item_count  = intval($page_item_count);
        $current_page_num = intval($current_page_num);
        $page_range_count = intval($page_range_count);
    
        if($total_item_count < 0){
            $total_item_count = 0;
        }
        if($page_item_count < 1 || $page_item_count > 1000){
            $page_item_count = 10;
        }
        if($current_page_num < 1){
            $current_page_num = 1;
        }
        if($page_range_count < 1  || $page_range_count > 20){
            $page_range_count = 5;
        }
        
        $page_data = $this->_get_page_data($total_item_count, $page_item_count, $current_page_num, $page_range_count);
        
        return $page_data;
    }
    
    protected function _get_page_data($total_item_count, $page_item_count, $current_page_num, $page_range_count)
    {                       
        $total_page_count = intval(ceil($total_item_count / $page_item_count));
        if($total_page_count > 0 && $current_page_num > $total_page_count){
            $current_page_num = $total_page_count;
        }
                
        $start_offset = ($current_page_num - 1) * $page_item_count;
        
        $previous_page_num = $current_page_num - 1;
        if ($previous_page_num < 1) {
            $previous_page_num = null;
        }
        $next_page_num = $current_page_num + 1;
        if ($next_page_num > $total_page_count) {
            $next_page_num = null;
        }
        
        $page_range = $this->_get_page_range($total_page_count, $current_page_num, $page_range_count);
        
        $page_data = array(
            'total_item_count' => $total_item_count,
            'page_item_count' => $page_item_count, 
            'total_page_count' => $total_page_count,
            
            'current_page_num' => $current_page_num,
            'start_offset' => $start_offset,
            
            'previous_page_num' => $previous_page_num,
            'next_page_num' => $next_page_num,
            
            'page_range' => $page_range,
        );
        
        return $page_data;
    }
    
    protected function _get_page_range($total_page_count, $current_page_num, $page_range_count)
    {       
        if ($page_range_count > $total_page_count) {
            $page_range_count = $total_page_count;
        }
        
        $delta = intval(ceil($page_range_count / 2));
        
        if ($current_page_num - $delta > $total_page_count - $page_range_count) {
            $page_range_first = $total_page_count - $page_range_count + 1;
            $page_range_last  = $total_page_count;
        } else {
            if ($current_page_num - $delta < 0) {
                $delta = $current_page_num;
            }
        
            $page_range_first = $current_page_num - $delta + 1;
            $page_range_last  = $page_range_first + $page_range_count - 1;
        }
               
        $page_range = range($page_range_first, $page_range_last);
        
        return $page_range;
    }
}
